package com.lanou3g.demo.controller.adminInfo;


import com.lanou3g.demo.common.ResultWrapper;
import com.lanou3g.demo.entity.AdminInfo;
import com.lanou3g.demo.entity.ex.AdminWithRole;
import com.lanou3g.demo.service.admininfo.IAdminInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 这是管理员模块的调节器
 */
@Controller
@RequestMapping("/admin")
public class AdminInfoController {

    private static IAdminInfoService adminInfoService;

    @Autowired
    public AdminInfoController(IAdminInfoService adminInfoService) {
        AdminInfoController.adminInfoService = adminInfoService;
    }


    // 查询所有管理员的信息和他们所对应的角色
    @RequestMapping("/adminList")
    @ResponseBody
    public ResultWrapper findAll(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "6") Integer count,
                                 String search) {
        List<AdminWithRole> adminInfoList = adminInfoService.findAll(page, count, search);
        int totalCount = adminInfoService.totalCount();
        return ResultWrapper.success(totalCount, adminInfoList);

    }

    // 删除按钮;删除角色对应的信息
    @RequestMapping("/adminDel")
    public void delAdmin(Integer adminId) {
        adminInfoService.deleteAdmin(adminId);
    }



    // 修改某个管理员的信息
    @RequestMapping("/adminModi")
    public String updateAdmin(AdminInfo adminInfo,String allRole){
        adminInfoService.updateAdmin(adminInfo,allRole);
        System.out.println("adminModi");
        return "redirect:admin_list.html";
    }


    // 增加按钮: 增加角色管理员的信息
    @RequestMapping("/adminAdd")
    public String adminAdd(AdminInfo adminInfo,String allRole){
        adminInfoService.addOneAdmin(adminInfo,allRole);
        return "redirect:admin_list.html";
    }

}
